#include <iostream>
#include <memory>
#include <string>
// 简单工厂模式
class Fruit
{
public:
    virtual void name() = 0;
};

class Apple : public Fruit
{
public:
    void name() override
    {
        std::cout << "这是一个苹果" << std::endl;
    }
};
class Banana : public Fruit
{
public:
    void name() override
    {
        std::cout << "这是一个香蕉" << std::endl;
    }
};

// class Fatroy
// {
// public:
// static std::shared_ptr<Fruit> create(const std::string name)
// {
// if (name == "苹果")
// {
// return std::make_shared<Apple>();
// }
// else if (name == "香蕉")
// {
// return std::make_shared<Banana>();
// }
// else
// {
// return nullptr;
// }
// }
// };

// class FruitFactory
// {
// public:
// virtual std::shared_ptr<Fruit> create() = 0;
// };
//
// class Applefactory : public FruitFactory
// {
// public:
// std::shared_ptr<Fruit> create() override
// {
// return std::make_shared<Apple>();
// }
// };
//
// class Bananafactory : public FruitFactory
// {
// public:
// std::shared_ptr<Fruit> create() override
// {
// return std::make_shared<Banana>();
// }
//
// };

class Animal
{
public:
    virtual void name() = 0;
};

class Dog : public Animal
{
public:
    void name() override
    {
        std::cout << "这是一个小狗" << std::endl;
    }
};
class Pig : public Animal
{
public:
    void name() override
    {
        std::cout << "这是一个小猪" << std::endl;
    }
};

class Factory
{
public:
    virtual std::shared_ptr<Fruit> GetFruit(const std::string name) = 0;
    virtual std::shared_ptr<Animal> GetAnimal(const std::string name) = 0;
};

class FruitFactory : public Factory
{
public:
    std::shared_ptr<Animal> GetAnimal(const std::string name) override
    {
        return nullptr;
    }
    std::shared_ptr<Fruit> GetFruit(const std::string name) override
    {
        if (name == "苹果")
        {
            return std::make_shared<Apple>();
        }
        else
        {
            return std::make_shared<Banana>();
        }
    }
};

class AnimalFactory : public Factory
{
public:
    std::shared_ptr<Fruit> GetFruit(const std::string name) override
    {
        return nullptr;
    }

    std::shared_ptr<Animal> GetAnimal(const std::string name)
    {
        if (name == "小狗")
        {
            return std::make_shared<Dog>();
        }
        else
        {
            return std::make_shared<Pig>();
        }
    }
};

class FactortProducer
{
public:
    static std::shared_ptr<Factory> create(const std::string &name)
    {
        if (name == "水果")
        {
            return std::make_shared<FruitFactory>();
        }
        else
        {
            return std::make_shared<AnimalFactory>();
        }
    }
};

int main()
{
    std::shared_ptr<Factory> f = FactortProducer::create("水果"); // 选择水果工厂
    std::shared_ptr<Fruit> fruit = f->GetFruit("苹果");           // 选择苹果
    fruit->name();
    fruit = f->GetFruit("香蕉");
    fruit->name();
    std::shared_ptr<Factory> ff = FactortProducer::create("动物");
    std::shared_ptr<Animal> animal = ff->GetAnimal("小猪");
    if (animal == nullptr)
        return 0;
    animal->name();
    animal = ff->GetAnimal("小狗");
    if (animal == nullptr)
        return 0;
    animal->name();
    // std::shared_ptr<FruitFactory> ff=std::make_shared<Applefactory>();
    // std::shared_ptr<Fruit>fruit = ff->create();
    // fruit->name();
    // ff.reset(new Bananafactory);
    // fruit=ff->create();
    // fruit->name();
    // std::shared_ptr<Fruit> apple = Fatroy::create("苹果");
    // apple->name();
    // std::shared_ptr<Fruit> banana = Fatroy::create("香蕉");
    // banana->name();
    return 0;
}